package com.example.mediamoudle.crop;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;



import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 视频 轨道的时间轴和轨道同步
 *
 * @auth yf
 * @date 2022/11/12 16:41
 */
public class RecyclerDivider extends RecyclerView.ItemDecoration {


    private Paint mPaint;
    private boolean isFirst = true;

    private SimpleDateFormat fmt = new SimpleDateFormat("mm:ss");

    public RecyclerDivider(int color) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        // 屏幕可见的数量
        final int childSize = parent.getChildCount();
        mPaint.setStrokeWidth(10);
        mPaint.setTextSize(20f);

        Point textSize = new Point();
        Rect bounds = new Rect();
        mPaint.getTextBounds("00:00", 0, 5, bounds);
        textSize.x = bounds.width();
        textSize.y = bounds.height();
        for (int i = 0; i < childSize; i++) {
            if (i ==childSize-1) continue;

            final View child = parent.getChildAt(i);
            int left = child.getLeft();
            int right = child.getRight();

            int index = parent.getChildAdapterPosition(child);
            if (index == 1) {
                // 绘制开始第0秒
                if (isFirst){
                    isFirst = false;
                    canvas.drawText("0:00", left - (textSize.x) / 2, (20 + textSize.y) / 2f, mPaint);
                    continue;
                }
            }
            if (index !=1 && index % 2 != 1) {
                // 绘制秒
                String timCurrent = fmt.format(new Date((index ) * 1000));
                canvas.drawText(timCurrent, right - (textSize.x) / 2, (20 + textSize.y) / 2f, mPaint);
            } else if (index>1 && index<parent.getAdapter().getItemCount()-1){
                // 绘制点
                canvas.drawCircle(right, (20 + 10) / 2f, 10 / 2f, mPaint);
            }
        }

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // 偏移量
        outRect.set(0, 80, 0, 0);
    }
}
